<?php
Class Category {
    //递归重新排序无限级分类数组(一维数组)
    Static Public function unlimitedForLevel($cate, $html='——', $pid=0, $level=0) {
        $arr = array();
        foreach($cate as $v) {
            if($v['pid'] == $pid) {
                $v['level'] = $level;
                // $v['html'] = str_repeat($html, $level);
                $v['html'] = $html;
                $arr[] = $v;
                $arr = array_merge($arr, self::unlimitedForLevel($cate, $html, $v['id'], $level+1));
            }
        }
        return $arr;
    }

    //递归重新排序无限级分类数组(多维数组)
    Static Public function unlimitedForLayer($cate, $name='child', $pid=0, $level=0) {
        $arr = array();
        foreach($cate as $v) {
            if($v['pid'] == $pid) {
                $v['level'] = $level;
                $v[$name] = self::unlimitedForLayer($cate, $name, $v['id'], $level+1);
                $arr[] = $v;
            }
        }
        return $arr;
    }

    //传递一个子分类ID返回所有的父级分类，包括自身
    Static Public function getParents($cate, $id) {
        $arr = array();
        foreach($cate as $v) {
            if($v['id'] == $id) {
                $arr[] = $v;
                $arr = array_merge(self::getParents($cate, $v['pid']), $arr);
            }
        }
        return $arr;
    }

    //传递一个父级分类ID返回所有"子级和子孙级分类"ID
    Static Public function getChildsId($cate, $pid) {
        $arr = array();
        foreach($cate as $v) {
            if($v['pid'] == $pid) {
                $arr[] = $v['id'];
                $arr = array_merge($arr, self::getChildsId($cate, $v['id']));
            }
        }
        return $arr;
    }

    //传递一个父级分类ID返回所有"子级和子孙级分类"
    Static Public function getChilds($cate, $pid) {
        $arr = array();
        foreach ($cate as $v) {
            if($v['pid'] == $pid) {
                $arr[] = $v;
                $arr = array_merge($arr, self::getChilds($cate, $v['id']));
            }
        }
        return $arr;
    }

    //传递一个父级分类ID返回所有"子级和子孙级分类"和"自身"，并且形成一个多维数组
    Static Public function getChildsTree($cate, $id) {
        //获取所有的子级分类（没有包括自身分类）
        $menuData1 = self::getChilds($cate, $id);

        //拼接加入自身分类        
        $menuData2 = M('SystemMenu')->where('id='.$id)->select();
        $menuData = array_merge($menuData1,$menuData2);

        //递归形成树状
        $arr = self::unlimitedForLayer($menuData, 'child', $menuData2[0]['pid']);

        return $arr;
    }


    //传递一个id获取其一级父级id
    Static Public function getTopParentid($id){ 
        $r = M('SystemMenu')->field('id,pid')->where('id='.$id)->find();
        if($r['pid'] != 1){
            return self::getTopParentid($r['pid']); 
        } 
        return $r['id'];  
    }  

    //传递一个id获取所有的父级id
    Static Public function getParentid($cate, $id){ 
        $tree = array();
        foreach($cate as $v) {
            if($v['id'] == $id) {// 判断要不要找父栏目
                if($v['pid'] > 0) { // parnet>0,说明有父栏目
                    $tree = array_merge($tree,self::getParentid($cate,$v['pid']));
                }
                $tree[] = $v; // 以找到上地为例
            }
        }
        return $tree;
    }  
}